package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.DestinationScreeningQuery;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.comment.service.ICantonCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.website.annotion.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.wolfcode.luowowo.comment.domain.CantonComment;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationScreeningService destinationScreeningService;
    @Reference
    private IScenicService scenicService;
    @Reference
    private ICantonCommentService cantonCommentService;

    @RequestMapping("")
    public String index(Model model){
        //hotRegions
        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);
        //monthDest
        List<DestinationScreening> months = destinationScreeningService.queryTheme((long) 0);
        //排除一月
        months.remove(0);
        model.addAttribute("months",months);
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){

        //查询所有目的地
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        int mid = list.size()/2;
        List<Destination> leftlist = list.subList(0, mid);
        List<Destination> rightList = list.subList(mid, list.size());

        model.addAttribute("leftDests",leftlist);
        model.addAttribute("rightDests",rightList);
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }


    //广州概览
    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.selectCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);
        //strategyDetails
        List<StrategyDetail> list = strategyDetailService.getViewnumTop3(id);
        model.addAttribute("strategyDetails",list);
        return "destination/guide";
    }
    //广州景点
    @RequestMapping("/list")
    public String list(Model model){
        List<List<Scenic>> scenics = new ArrayList<>(); //用于储存通过id查询出的景点
        List<List<Scenic>> imgList = new ArrayList<>(); //用于储存通过id查询出的图片
        List<Scenic> list = scenicService.selectAll(); //查询所有
        model.addAttribute("list",list);
        for(Scenic s : list){
            Long id = s.getId(); //循环遍历出 景区的id
            List<Scenic> scenicList = scenicService.selectById(id); //通过景区的id查询下面所有的景点
            scenics.add(scenicList);   //添加到存储景点的集合中
            List<Scenic> img = scenicService.selectByimgId(id);  //通过景区的id查询下面所有的图片
            imgList.add(img);  //添加到存储图片的集合中
        }
        model.addAttribute("scenics",scenics);
        model.addAttribute("imgList",imgList);
        return "destination/list";
    }
    //广州景点点评
    @RequestMapping("/detail")
    public String detail(Model model, Long id){
        Scenic scenic = scenicService.selectByPrimaryKey(id);//通过id查询出对象
        List<Scenic> img = scenicService.selectByimgId(id);  //通过id查询出所有图片
        Long imgCount = scenicService.selectByimgIdCount(id); //通过id查询出所有图片的个数
        model.addAttribute("id",id);
        model.addAttribute("scenic",scenic);
        model.addAttribute("img",img);
        model.addAttribute("imgCount",imgCount);

        return "destination/detail";
    }
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(HttpServletResponse response,CantonComment comment ){
        cantonCommentService.save(comment);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo")StrategyCatalogQuery qo){
        //获取catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.selectCatalogByDestId(qo.getDestId());
        StrategyCatalog catalog=null;
        if(catalogs !=null && catalogs.size()>0){
            //后去catalog
            for (StrategyCatalog c : catalogs) {
                if(c.getId() == qo.getCatalogId()){
                    catalog = c;
                    model.addAttribute("catalog",c);
                }
            }
        }
        model.addAttribute("catalogs",catalogs);
        //获取detail
        if(catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if(details != null && details.size() > 0){
                StrategyDetail detail = details.get(0);
                StrategyContent content = strategyDetailService.getContent(detail.getId());
                detail.setStrategyContent(content);
                model.addAttribute("detail",detail);
            }
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model,@ModelAttribute("qo") TravelQuery qo){

        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }

    @RequestMapping("/destFilter")
    public String DestinationSearch(Model model, Long id, Long category){
        //times
        List<DestinationScreening> months = destinationScreeningService.queryTheme((long) 0);
        List<DestinationScreening> festivals = destinationScreeningService.queryTheme((long) 1);
        model.addAttribute("months",months);
        model.addAttribute("festivals",festivals);
        //themes
        List<DestinationScreening> allyears = destinationScreeningService.queryTheme((long) 2);
        List<DestinationScreening> seasons = destinationScreeningService.queryTheme((long) 3);
        List<DestinationScreening> ways = destinationScreeningService.queryTheme((long) 4);
        model.addAttribute("allyears",allyears);
        model.addAttribute("seasons",seasons);
        model.addAttribute("ways",ways);
        //days
        List<DestinationScreening> days = destinationScreeningService.queryTheme((long) 5);
        model.addAttribute("days",days);
        //destList
        System.out.println("id="+id);
        System.out.println("category="+category);
        model.addAttribute("screeningId",id);
        model.addAttribute("category",category);
        return "destination/destFilter";
    }

    @RequestMapping("/getThemeByCategory")
    public String getThemeByCategory(Model model,Long category){
        List<DestinationScreening> themeFilters = destinationScreeningService.queryTheme(category);
        List<DestinationScreening> list = new ArrayList<>();
        for (DestinationScreening themeFilter : themeFilters) {
            if(themeFilter.getCoverUrl() != null){
                list.add(themeFilter);
            }
        }
        model.addAttribute("themeFilters",list);
        model.addAttribute("category",category);
        return "destination/themeFilterTpl";
    }

    @RequestMapping("/getHotDestByMonth")
    public String getHotDestByMonth(Model model,Long monthId){
        //monthDests
        List<DestinationScreening> destinationScreenings = destinationScreeningService.queryDest(monthId);
        List<Destination> monthDests = new ArrayList<>();
        if(destinationScreenings != null) {
            for (DestinationScreening monthDest : destinationScreenings) {
                if(monthDest != null){
                    monthDests.add(monthDest.getDest());
                }
            }
            model.addAttribute("monthDests", monthDests);
        }
        //monthId
        model.addAttribute("monthId", monthId);
        //
        return "destination/monthdestTpl";
    }

    @RequestMapping("/getDestByScreening")
    public String getDestByScreening(Model model,@ModelAttribute("qo") DestinationScreeningQuery qo){
        //目的地筛选
        List<Destination> destList = destinationService.queryScreening(qo);
        if(destList != null){
            model.addAttribute("destList",destList);
        }
        return "destination/destFilterTpl";
    }

}
